<?php

class Az_Pager {

    var $url = null;
    var $total_record = null;
    var $total_page = null;
    var $scoll_page = null;
    var $per_page = null;
    var $curent_page = null;
    var $start = null;
    var $page_link = null;
    var $previous_page = null;
    var $next_page = null;
    var $firt_page = null;
    var $last_page = null;
    var $page_info = null;

    function total_pages($url, $total_record, $scoll_page, $per_page, $curent_page) {
        $this->url = $url;
        $this->total_record = $total_record;
        $this->scoll_page = $scoll_page;
        $this->per_page = $per_page;
        if (!is_numeric($curent_page)) {
            $this->curent_page = 1;
        } else {
            $this->curent_page = $curent_page;
        }
        if ($this->curent_page == 1) {
            $this->start = 0;
        } else {
            $this->start = ($this->curent_page - 1) * $this->per_page;
        }
        $this->total_page = ceil($this->total_record / $this->per_page);
    }

    //thiet lap link cho cac nut chuyen trang
    function page_link($inaction_page, $page_url_lart, $class_li) {
        if ($this->total_page <= $this->scoll_page) {
            if ($this->total_record <= $this->per_page) {
                $loop_start = 1;
                $loop_finish = $this->total_page;
            } else {
                $loop_start = 1;
                $loop_finish = $this->total_page;
            }
        } else {
            if ($this->curent_page < intval(($this->scoll_page / 2) + 1)) {
                $loop_start = 1;
                $loop_finish = $this->scoll_page;
            } else {
                $loop_start = $this->curent_page - intval($this->scoll_page / 2);
                $loop_finish = $this->curent_page + intval($this->scoll_page / 2);
                if ($loop_finish > $this->total_page) {
                    $loop_finish = $this->total_page;
                }
            }
        }
        for ($i = $loop_start; $i <= $loop_finish; $i++) {
            if ($i == $this->curent_page) {
                $this->page_link.='<li ' . $inaction_page . '><span>' . $i . '</span></li>';
            } else {
                $this->page_link.='<li><a href="' . $this->url . $i . $page_url_lart . '" class="' . $class_li . '">' . $i . '</a></li>';
            }
        }
    }

    function previous_page($page_url_last) {
        if ($this->curent_page > 1) {
            $this->previous_page = '<a href="' . $this->url . ($this->curent_page - 1) . $page_url_last . '" class="page-left"></a>';
        }
    }

    function next_page($page_url_last) {
        if ($this->curent_page < $this->total_page) {
            $this->next_page = '<a href="' . $this->url . ($this->curent_page + 1) . $page_url_last . '" class="page-right"></a>';
        }
    }

    function firt_page($page_url_last) {
        if ($this->curent_page > 1) {
            $this->firt_page = '<a href="' . $this->url . '1' . $page_url_last . '" class="page-far-left"></a>';
        }
    }

    function last_page($page_url_last) {
        if ($this->curent_page < $this->total_page) {
            $this->last_page = '<a href="' . $this->url . $this->total_page . $page_url_last . '" class="page-far-right"></a>';
        }
    }

    function previous_page_azplugin() {
        if ($this->curent_page > 1) {
            $this->next_page = '<a href="#" onclick="load_data(' . ($this->curent_page - 1) . ');" >Back</a>';
        }
    }

    function next_page_azplugin() {
        if ($this->curent_page < $this->total_page) {
            $this->next_page = '<a href="#" onclick="load_data(' . ($this->curent_page + 1) . ');" >Next</a>';
        }
    }

    function firt_page_azplugin() {
        if ($this->curent_page > 1) {
            $this->firt_page = '<a href="#" onclick="load_data(1);">First</a>';
        }
    }

    function last_page_azplugin() {
        if ($this->curent_page < $this->total_page) {
            $this->last_page = '<a href="#" onclick="load_data(' . $this->total_page . ');">Last</a>';
        }
    }

    function page_info() {
        $this->page_info = '<div id="page-info">Page <strong>' . $this->curent_page . '</strong> / ' . $this->total_page . '</div>';
    }

    function page_set($url, $total_record, $scroll_page, $per_page, $current_page) {
        $this->total_pages($url, $total_record, $scroll_page, $per_page, $current_page);
        // $this->page_link($inaction_page,$page_url_lart);
        $this->previous_page($page_url_last);
        $this->next_page($page_url_last);
        $this->page_info();
        $this->firt_page($page_url_last);
        $this->last_page($page_url_last);
    }

    function page_set_azplugin($url, $total_record, $scroll_page, $per_page, $current_page) {
        $this->total_pages($url, $total_record, $scroll_page, $per_page, $current_page);
        // $this->page_link($inaction_page,$page_url_lart);
        $this->previous_page_azplugin();
        $this->next_page_azplugin();
        $this->page_info();
        $this->firt_page_azplugin();
        $this->last_page_azplugin();
    }

}

?>